﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using System.Data;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmThemNguoiDung.xaml
    /// </summary>
    public partial class frmThemNguoiDung : FormTrans
    {
        public bool UpdateSucces = false;
        DataTable Data = new DataTable();

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemNguoiDung), new UIPropertyMetadata(true));



        public DataTable tbNhanVien
        {
            get { return (DataTable)GetValue(tbNhanVienProperty); }
            set { SetValue(tbNhanVienProperty, value); }
        }

        // Using a DependencyProperty as the backing store for tbNhanVien.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty tbNhanVienProperty =
            DependencyProperty.Register("tbNhanVien", typeof(DataTable), typeof(frmThemNguoiDung), new UIPropertyMetadata(null));


        Command cm;

        public frmThemNguoiDung()
        {
            InitializeComponent();
        }


        public frmThemNguoiDung(DataTable tb, Command _cm)
        {
            InitializeComponent();
            IsEditMode = _cm == Command.View ? false : true;
            cbbNhom.ItemsSource = ViewModels.NguoiDungViewModel.GetNhomNguoiDung().DefaultView;
            tbNhanVien = ViewModels.NhanVienViewModel.GetAllNhanVien("manv,tennv,cmnd");
            cm = _cm;
            if (tb == null)
                Data = ViewModels.NguoiDungViewModel.CreateNewNguoiDung();
            else
                Data = tb.Copy();
            gridMain.DataContext = Data.DefaultView;
        }

        //Kiểm tra dữ liệu hợp lệ
        private bool KiemTra()
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập mã":"please input user id", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtUserName.Focus();
                return false;
            }
            else
                if (string.IsNullOrEmpty(txtPassword.Password))
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập mật khẩu":"please input password", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtPassword.Focus();
                    return false;
                }
                else
                    if (string.IsNullOrEmpty(txtRePassword.Password))
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Hãy nhập lại mật khẩu":"please confirm password", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtRePassword.Focus();
                        return false;
                    }
                    else
                        if (string.IsNullOrEmpty(cbbNhom.Text))
                        {
                            MessageBox.Show(M_LAN.Equals("V")?"Chưa chọn nhóm":"please input group", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                            cbbNhom.Focus();
                            return false;
                        }
                        else
                            if (string.IsNullOrEmpty(txtNhanVien.ValueItem.ToString()))
                            {
                                MessageBox.Show(M_LAN.Equals("V")?"Chưa chọn nhân viên":"please chosse staff", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                                txtNhanVien.GetFocus();
                                return false;
                            }
                            else
                                if (!txtPassword.Password.Equals(txtRePassword.Password))
                                {
                                    MessageBox.Show(M_LAN.Equals("V")?"Mật khẩu nhập lại không khớp":"Comfirm password not match", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                                    txtRePassword.Focus();
                                    return false;
                                }
            return true;
        }

        //FormLoad
        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {

            if (IsEditMode)
            {
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
                tbMain.Cm_New+=new ucXToolBar.Execute(tbMain_Cm_New);
            }

            txtUserName.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
        }

        //Thêm
        private void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            if (KiemTra())
            {
                Data.Rows[0]["password"] = Models.DataProviderModel.MaHoaMatKhau(txtPassword.Password);
                if (ViewModels.NguoiDungViewModel.ThemNguoiDung(Data))
                {
                    this.UpdateSucces = true;
                    if (cm == Command.New)
                        MessageBox.Show(M_LAN.Equals("V")?"Thêm người dùng thành công!":"Insert user successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    if (cm == Command.Edit)
                        MessageBox.Show(M_LAN.Equals("V")?"Cập nhật người dùng thành công!":"Update user successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    
                }
            }
        }

        //Đóng form
        public void tbMain_Cm_Exit()
        {
            this.Close();
        }

        public void tbMain_Cm_New()
        {
            Data = ViewModels.KhachHangViewModel.CreateNewKhachHang();
            gridMain.DataContext = Data.DefaultView;
            txtUserName.Focus();
        }

        private void txtTextboxComplete_AddNewItem(object sender, RoutedEventArgs e)
        {
            frmThemNhanVien frm = new frmThemNhanVien(null,Command.New);
            frm.ShowDialog();
            if (frm.UpdateSucces)
                tbNhanVien = ViewModels.NhanVienViewModel.GetAllNhanVien("manv,tennv,cmnd");
        }

        private void cbbNhom_GotFocus(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb != null)
                cb.IsDropDownOpen = true;
        }
    }
}
